//
//  graph.m
//  Eath
//
//  Created by yuhaya on 10/4/14.
//  Copyright (c) 2014 yuhaya. All rights reserved.
//

#import "graph.h"
#import "Pointer.h"
#import "Tool.h"
#import "picture.h"


@implementation graph

- (id)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        // Initialization code
    }
    return self;
}

- (id)initWithFrame:(CGRect)frame andStu:(Student *)stu andTime:(NSUInteger)time
{
    self = [super initWithFrame:frame];
    if (self) {
        //self.backgroundColor = [UIColor whiteColor];
        self.opaque = NO;
        self.stu = stu;
        self.time = time;
        self.date_name = @[@"08:00",@"12:00",@"13:30",@"17:30",@"19:00",@"21:00",@"24:00"];
        [self.stu all_mes:self.time];
        self.date_point = [NSMutableArray array];
        self.position_point = [NSMutableArray array];
        self.school_point = [NSMutableArray array];
        [self set_time_point];
        // Initialization code
    }
    return self;
}

- (void)set_time_point
{
    
    NSUInteger stand_len = self.date_name.count;
    NSUInteger point_len = self.date_point.count;
    
   NSMutableArray * stand_pos = [NSMutableArray arrayWithCapacity:stand_len];
   NSMutableArray * point_pos = [NSMutableArray arrayWithCapacity:point_len];
   NSMutableArray * origin_total_pos = [NSMutableArray arrayWithCapacity:stand_len + point_len];
    
    
    for (int a=0; a<stand_len; a++) {
        //预设坐标转换
        stand_pos[a] = [self time_str_num:self.date_name[a]];
        [origin_total_pos addObject:stand_pos[a]];
    }
    
    for (int a=0; a<point_len; a++) {
        point_pos[a] = [self time_str_num:self.date_point[a]];
        [origin_total_pos addObject:point_pos[a]];
    }
    
    DLog(@"%@",origin_total_pos);
    
   NSArray * new_total_pos = [origin_total_pos sortedArrayUsingComparator:^(NSNumber * obj1, NSNumber * obj2) {
       
       if ([obj1 integerValue] > [obj2 integerValue]) {
           return (NSComparisonResult)NSOrderedDescending;
       }
       
       if ([obj1 integerValue] < [obj2 integerValue]) {
           return (NSComparisonResult)NSOrderedAscending;
       }
       return (NSComparisonResult)NSOrderedSame;
       
   }];
    
    NSMutableArray * position_total = [NSMutableArray array];//对应所有点的坐标
    NSMutableArray * position_stand = [NSMutableArray array];//预设坐标
    NSMutableArray * position_point = [NSMutableArray array];//时间点坐标
    
    for (int a=0; a<[new_total_pos count]; a++) {
        
        int h = (int)(a * SPACE) + 60;
        NSNumber * point = [NSNumber numberWithInt:h];
        [position_total addObject:point];
        
        if ([stand_pos indexOfObject:new_total_pos[a]] != NSNotFound) {
            [position_stand addObject:point];
        }
        
        if ([point_pos indexOfObject:new_total_pos[a]] != NSNotFound) {
            [position_point addObject:point];
        }
    }
    
    self.position_point = position_point;
    self.position_stand = position_stand;
    self.position_total = position_total;
    
}


//- (void)time_point_change
//{
//    
//    NSMutableArray * data = [NSMutableArray arrayWithCapacity:self.date_name.count];
//    NSUInteger len = self.date_name.count;
//    NSUInteger len2 = self.date_point.count;
//    
//    for (int a=0; a<len; a++) {
//        //预设坐标转换
//        data[a] = [self time_str_num:self.date_name[a]];
//    }
//    
//    for (int a = 0; a<len2; a++) {
//        //时间点的坐标
//        NSNumber * point = [Tool time_str_num:self.date_point[a]];
//        
//        for (int b = 0; b<len; b++) {
//            //预设的坐标
//            NSComparisonResult res = [point compare:data[b]];
//            
//            if(res == NSOrderedAscending){
//                
//                NSNumber * tmp_time = data[b];
//                NSInteger time_s = 0;
//                NSInteger time_e = tmp_time.integerValue;
//                NSInteger s_y = start.y;
//                NSInteger e_y = time_point[b].y;
//                
//                if(b != 0){
//                    
//                    NSNumber * tmp_time_before = data[b-1];
//                    time_s = tmp_time_before.integerValue;
//                    s_y = time_point[b-1].y;
//                }
//                NSInteger y = (point.integerValue - time_s) * (e_y - s_y) / (time_e - time_s) + s_y;
//                
//                if(labs(y-e_y) < 10){
//                    y = y > e_y ? y + 15 : y - 15;
//                }
//                if(y-s_y < 10){
//                    y = y + 15;
//                }
//                if(a != 0){
//                    NSNumber * pre = self.position_point[a-1];
//                    if(labs(y-pre.integerValue) < 10){
//                        
//                        y = pre.integerValue + 15;
//                    }
//                }
//                self.position_point[a] = [NSNumber numberWithInteger:y];
//                break;
//                
//            }else if (res == NSOrderedSame){
//                
//                NSInteger y = time_point[b].y;
//                
//                if(a != 0){
//                    NSNumber * pre = self.position_point[a-1];
//                    if(labs(y-pre.integerValue) < 10){
//                        
//                        y = pre.integerValue + 15;
//                    }
//                }
//                self.position_point[a] = [NSNumber numberWithInteger:y];
//                break;
//                
//            }else{
//                
//                continue;
//            }
//        }
//    }
//    
//}
-(NSNumber *)time_str_num:(NSString *)time
{
    NSString * hours = [time substringWithRange:NSMakeRange(0, [time rangeOfString:@":"].location)];
    NSString * minutes = [time substringFromIndex:[time rangeOfString:@":"].location + 1];
    NSInteger mins = hours.integerValue * 60 + minutes.integerValue;
    return [NSNumber numberWithInteger:mins];
}

- (void)drawRect:(CGRect)rect

{
    static int start_x = 75;
    static int start_y = 0;
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    CGPoint point_start = CGPointMake(start_x, start_y);
    DLog(@"%@",self.position_total);
    CGPoint point_end = CGPointMake(start_x, self.position_total.count * SPACE);
    CGContextSetLineWidth(ctx,3);
    CGContextSetRGBStrokeColor(ctx, 0.110, 0.573, 0.980, 1);
    CGContextMoveToPoint(ctx, point_start.x, point_start.y);
    CGContextAddLineToPoint(ctx, point_end.x, point_end.y);
    CGContextClosePath(ctx);
    CGContextStrokePath(ctx);
    
    
    
    //画点
    UIColor*aColor = [UIColor colorWithRed:1 green:1 blue:1 alpha:1];
    CGContextSetLineWidth(ctx, 3.0);//线的宽度
    UIFont * font = [UIFont boldSystemFontOfSize:13.0];//设置字体大小
    UIFont * font_error = [UIFont boldSystemFontOfSize:12.0];
    
    int img_count = 0;
    
    for (int a = 0; a < [self.position_stand count]; a++) {
        
            int tmp_y = [[self.position_stand objectAtIndex:a] intValue];
            
            CGContextSetRGBStrokeColor(ctx, 0.110, 0.573, 0.980, 1);
            CGContextAddArc(ctx, start_x, tmp_y, 5, 0, 2*PI, 0); //添加一个圆
            CGContextSetFillColorWithColor(ctx, aColor.CGColor);//填充颜色
            //        CGContextSetShadowWithColor(ctx, CGSizeMake(0, 0), 1, [[UIColor blackColor] CGColor]);
            CGContextDrawPath(ctx, kCGPathFillStroke); //绘制路径加填充
            
            CGContextSetRGBFillColor (ctx,  0.467, 0.467, 0.565, 1.0);
            [self.date_name[a] drawInRect:CGRectMake(20, tmp_y - 8, 80, 20) withFont:font];
            
            if(img_count == 0){
                
                UIImage *image1 = [UIImage imageNamed:@"sun1"];
                [image1 drawInRect:CGRectMake(15, tmp_y-40, 32, 32)];
            }else if(img_count == 1){
                
                UIImage *image2 = [UIImage imageNamed:@"sun2"];
                [image2 drawInRect:CGRectMake(15, tmp_y+20, 30, 30)];
                
            }else if (img_count == 3){
                
                UIImage *image3 = [UIImage imageNamed:@"sun3"];
                [image3 drawInRect:CGRectMake(15, tmp_y-30, 30, 30)];
            }else if (img_count == 4){
            
                UIImage *image4 = [UIImage imageNamed:@"sun4"];
                [image4 drawInRect:CGRectMake(15, tmp_y+10, 30, 30)];
            }else if (img_count == 6){
                
                UIImage *image5 = [UIImage imageNamed:@"sun5"];
                [image5 drawInRect:CGRectMake(15, tmp_y-50, 30, 30)];
            }
            
            img_count ++;
    }
    
    
    UIColor*bColor = [UIColor greenColor];
    CGContextSetLineWidth(ctx, 2.0);
    
    for (int a=0; a<self.position_point.count; a++) {
        
        NSNumber * position = self.position_point[a];
        CGContextSetRGBStrokeColor(ctx, 0.110, 0.573, 0.980, 1);
        CGContextSetLineWidth(ctx,2);
        CGContextAddArc(ctx, start_x, position.integerValue, 4, 0, 2*PI, 0); //添加一个圆
        CGContextSetFillColorWithColor(ctx, bColor.CGColor);//填充颜色
        CGContextDrawPath(ctx, kCGPathFillStroke); //绘制路径加填充
        CGContextSetRGBFillColor (ctx,  0.467, 0.467, 0.565, 1.0);
        NSString * time_tmp = [self.date_point[a] substringToIndex:5];
        [time_tmp drawInRect:CGRectMake(100, position.integerValue - 8, 80, 20) withAttributes:@{NSFontAttributeName:font}];
        //[time_tmp drawInRect:CGRectMake(100, position.integerValue - 8, 80, 20) withFont:font];
        
        CGPoint point_start = CGPointMake(140, position.integerValue);
        CGPoint point_end = CGPointMake(220, position.integerValue);
        CGContextSetLineWidth(ctx,1);
        CGContextSetRGBStrokeColor(ctx, 0.110, 0.573, 0.980, 1);
        CGContextMoveToPoint(ctx, point_start.x, point_start.y);
        CGContextAddLineToPoint(ctx, point_end.x, point_end.y);
        CGContextClosePath(ctx);
        CGContextStrokePath(ctx);
        
//        if([self.equipment_status[a] integerValue] == 0){
//           CGContextSetRGBFillColor (ctx,  0.467, 0.467, 0.565, 1.0);
//            NSString * error = @"设备检修中";
//            [error drawInRect:CGRectMake(148, position.integerValue - 22, 80, 20) withAttributes:@{NSFontAttributeName:font_error,NSForegroundColorAttributeName:[UIColor redColor]}];
//        }
        
        CGContextSetRGBFillColor (ctx,  0.467, 0.467, 0.565, 1.0);
        [self.school_point[a] drawInRect:CGRectMake(240, position.integerValue - 8, 80, 20) withFont:font];
    }
    
}

@end
